/**
 * 
 */
package DAO;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entities.MWorks;
import entities.Works;
import ultility.ConnectDatabase;
import ultility.Procedure;

/**
 * @author NhatHung
 *
 */
public class WorkDAO {
	static Connection con = ConnectDatabase.makeConnect();
	static CallableStatement cs = null;
	static ResultSet rs = null;
	static int pageCount = 0;

	// Post: save work to database and waiting decide: success return true or
	// fail return false
	public static boolean post(Works w) {
		try {
			cs = con.prepareCall(Procedure.SQL_SP_POST);
			cs.setInt(1, w.getLocation());
			cs.setInt(2, w.getRecruiterId());
			cs.setInt(3, w.getWorkType());
			cs.setInt(4, w.getPosition());
			cs.setString(5, w.getSalary());
			cs.setString(6, w.getDescription());
			cs.setString(7, w.getRequiredSex());
			cs.setString(8, w.getRequiredAge());
			cs.setString(9, w.getRequiredLiteracy());
			cs.setInt(10, w.getRequiredNumber());
			cs.setString(11, w.getRequiredLanguage());
			cs.setString(12, w.getRequiredAnother());
			cs.setString(13, w.getPhone());
			cs.setString(14, w.getEmail());
			cs.setString(15, w.getAddress());
			cs.executeQuery();
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
			return false;
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	// Update: edit work information: success return true or fail return false
	public static boolean update(Works w, int fixerId) {
		try {
			cs = con.prepareCall(Procedure.SQL_SP_WORK_UPDATE);
			cs.setInt(1, w.getWorkId());
			cs.setInt(2, w.getLocation());
			cs.setInt(3, w.getWorkType());
			cs.setInt(4, w.getPosition());
			cs.setString(5, w.getSalary());
			cs.setString(6, w.getDescription());
			cs.setString(7, w.getRequiredSex());
			cs.setString(8, w.getRequiredAge());
			cs.setString(9, w.getRequiredLiteracy());
			cs.setInt(10, w.getRequiredNumber());
			cs.setString(11, w.getRequiredLanguage());
			cs.setString(12, w.getRequiredAnother());
			cs.setString(13, w.getPhone());
			cs.setString(14, w.getEmail());
			cs.setString(15, w.getAddress());
			cs.setInt(16, fixerId);
			cs.executeQuery();
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
			return false;
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	// get list will display in main page
	public static List<MWorks> getListMain(int pageIndex, int pageSize) {
		List<MWorks> list = new ArrayList<MWorks>();
		try {
			cs = con.prepareCall(Procedure.SQL_SP_LOAD_PAGE);
			cs.setInt(1, pageIndex);
			cs.setInt(2, pageSize);
			rs = cs.executeQuery();
			pageCount = cs.getInt(3);
			while (rs.next()) {
				MWorks mW = new MWorks(rs.getInt(1), rs.getString(2),
						rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9));
				list.add(mW);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
			return null;
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	// get list search
	public static List<MWorks> getListSearch(String searchKey, int workType,
			int location, int pageIndex, int pageSize) {
		List<MWorks> list = new ArrayList<MWorks>();
		try {
			cs = con.prepareCall(Procedure.SQL_SP_WORK_UPDATE);
			cs.setString(1, searchKey);
			cs.setInt(2, workType);
			cs.setInt(3, location);
			cs.setInt(4, pageIndex);
			cs.setInt(5, pageSize);
			rs = cs.executeQuery();
			pageCount = cs.getInt(6);
			while (rs.next()) {
				MWorks mW = new MWorks(rs.getInt(1), rs.getString(2),
						rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9));
				list.add(mW);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
			return null;
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
}
